"use strict";
const path = require("path");
const webpack = require("webpack");

function resolve(dir) {
  return path.join(__dirname, dir);
}

console.log(`\n\t\x1B[1m当前运行环境：\x1B[22m \x1B[32m${process.env.VUE_APP_ENV}\x1B[39m\n`);

module.exports = {
  publicPath: "./",
  lintOnSave: false,
  devServer: {
    disableHostCheck: true,
    port: 8080,
    open: true,
    overlay: {
      warnings: false,
      errors: true,
    },
    proxy: {
      "/API": {
        target: process.env.VUE_APP_SERVER,
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          "^/API": "",
        },
      },
    },
  },
  // 主题配置
  css: {
    loaderOptions: {
      less: {
        lessOptions: {
          modifyVars: {
            "primary-color": "#1B69FD",
            "link-color": "#1B69FD",
            "warning-color": "#faad14",
            "success-color": "#52c41a",
            "error-color": "#f5222d",
            "heading-color": "rgba(0, 0, 0, 0.85)",
            "text-color": "rgba(0, 0, 0, 0.65)",
            "disabled-color": "rgba(0, 0, 0, 0.25)",
          },
          javascriptEnabled: true,
        },
      },
    },
  },
  // configureWebpack: {
  // },
  chainWebpack(config) {
    config.resolve.symlinks(true);
    config
      .plugin("ignore")
      // 忽略/moment/locale下的所有文件
      .use(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/));
    config.plugin("preload").tap(() => [
      {
        rel: "preload",
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: "initial",
      },
    ]);
    config.plugins.delete("prefetch");
    config.module
      .rule("svg")
      .exclude.add(resolve("src/icons"))
      .end();
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(resolve("src/icons"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]",
      })
      .end();

    config.when(process.env.NODE_ENV !== "development", (config) => {
      config
        .plugin("ScriptExtHtmlWebpackPlugin")
        .after("html")
        .use("script-ext-html-webpack-plugin", [
          {
            // `runtime` must same as runtimeChunk name. default is `runtime`
            inline: /runtime\..*\.js$/,
          },
        ])
        .end();
      config.optimization.splitChunks({
        chunks: "all",
        cacheGroups: {
          libs: {
            name: "chunk-libs",
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: "initial",
          },
          elementUI: {
            name: "chunk-ant",
            priority: 20,
            test: /[\\/]node_modules[\\/]_?antd(.*)/,
          },
          commons: {
            name: "chunk-commons",
            test: resolve("src/components"),
            minChunks: 3,
            priority: 5,
            reuseExistingChunk: true,
          },
        },
      });
      config.optimization.runtimeChunk("single");
    });
  },
};
